#!/bin/bash

set -eu

BUILD_DEBUG="${BUILD_DEBUG:-}"
if [[ -n "${BUILD_DEBUG}" ]]; then
    set -x
fi

# cd to app root
CWD=$(dirname $0)
if [[ `basename $(pwd)` = 'scripts' ]]; then
  cd ../
else
  cd `dirname $CWD`
fi

source ./scripts/utils.sh

# -------------------------------------

function printHelp() {
    cat 1>&2 <<EOF
build-static Usage:
  -d - Build debug instead of production build
  -f - Force: Turn off checks that prevent you from doing bad things
  -c=CDN - Force a CDN path for static asset delivery. Default <releases.rancher.com/ui>
  -v=VERSION - Force version to be VERSION instead of what is in package.json
EOF
  exit 1;
}

# -------------------------------------

# Parse options
BRANCH=$CI_BRANCH
BUILD_TAG="${CI_BUILD_TAG#v}"
CDN="releases.rancher.com/ui"

if [[ -z "$BUILD_TAG" ]]; then
  PKG_VERSION=$(cat package.json | grep version | head -n 1 | cut -f4 -d'"')
else
  PKG_VERSION="$BUILD_TAG"
fi

FORCE=0
LATEST=0
MODE=""
ENVIRONMENT="production"
FINGERPRINT="yes"
: ${FORCE_VERSION:=""}

while getopts ":dlustfv:c:p:m:" opt;do
    case $opt in
    d)
        ENVIRONMENT="development"
        ;;
    f)
        FORCE=1
        ;;
    m)
        MODE=$OPTARG
        ;;
    v)
        FORCE_VERSION=$OPTARG
        ;;
    c)
        CDN=$OPTARG
        ;;
    \?)
        echo "Invalid arguemnt: ${OPTARG}"
        printHelp
        exit 1
        ;;
    :)
        echo "Option -${OPTARG} requires an argument." >&2
        printHelp
        exit 1
        ;;
    esac
done

# Why are you trying to do a build when there are uncommitted changes?
if [[ `git status --porcelain` ]]; then
  echo "There are uncommited changes.  Please check the number and try again."
  git status
  if [[ $FORCE -ne 1 ]]; then
    exit 1;
  fi
fi

# If we're on master and building from branch ... we're building latest
if [[ "${CI_BRANCH}" == "master" ]] ; then
  LATEST=1
fi

if [[ "${FORCE_VERSION}" != "" ]]; then
  VERSION=${FORCE_VERSION}
else
  if [[ $LATEST -eq 1 ]]; then
    VERSION="latest2"
  elif [[ "${CI_BRANCH}" != "" ]]; then
    VERSION=${CI_BRANCH}
  else
    VERSION=${PKG_VERSION}
  fi
fi

if [[ $LATEST -eq 1 ]]; then
  FINGERPRINT="no"
fi

BUILD_PAR="dist/static"
BUILD_DIR="${BUILD_PAR}/${VERSION}"
BUILD_TGZ="${BUILD_PAR}/${VERSION}.tar.gz"

# Outputs for gh actions
ENV_OUTPUT="${GITHUB_OUTPUT:-"temp-env"}"
echo "BUILD_DIR=${BUILD_DIR}" >> "$ENV_OUTPUT"
echo "BUILD_TGZ=${BUILD_TGZ}" >> "$ENV_OUTPUT"
echo "VERSION=${VERSION}" >> "$ENV_OUTPUT"

# Print settings
echo "Branch: ${BRANCH}"
echo "Package Version: ${PKG_VERSION}"
echo "Version: ${VERSION}"
echo "Build Dir: ${BUILD_DIR}"
echo "Options: Force=${FORCE}, Mode=${MODE}, Latest=${LATEST}"
echo "Current Directory: $(pwd)"

if [[ $LATEST -ne 1 ]]; then
  echo "Building Static Tarball..."
  UI_MODE="${MODE}" RANCHER="" CATALOG="" runCmd ./node_modules/.bin/ember build --environment=${ENVIRONMENT} --output-path=${BUILD_DIR}

  # Create a file containing the version
  echo "${PKG_VERSION}" > ${BUILD_DIR}/VERSION.txt

  # Remove .DS_Store files
  runCmd find ${BUILD_DIR} -name '.DS_Store' -exec rm {} \;

  # Create a tarball of the version
  runCmd tar -czf ${BUILD_TGZ} -C ${BUILD_PAR} ${VERSION}
fi;

echo "Building Hosted Version..."
UI_MODE="${MODE}" FINGERPRINT="${FINGERPRINT}" RANCHER="" CATALOG="" BASE_ASSETS="//${CDN}/${VERSION}/" runCmd ./node_modules/.bin/ember build --environment=${ENVIRONMENT} --output-path=${BUILD_DIR}

# Create a file containing the version
echo "${PKG_VERSION}" > ${BUILD_DIR}/VERSION.txt
